//
//  SearchClassifyCollectionReusableView.swift
//  LeTu
//
//  Created by g1 on 2024/1/15.
//

import UIKit

class SearchClassifyCollectionReusableView: UICollectionReusableView {
        
    private let titles = ["国内".localized, "海外".localized]
    var selectClassifyHandle: ((String, Int)->Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    
    // MARK: - configUI
    func configUI() {
        
        addSubview(classifySegmentedView)
        
        classifySegmentedView.snp.makeConstraints { make in
            make.top.equalTo(6)
            make.centerX.equalToSuperview()
            make.width.equalTo(LanguageManager.shared.currentLanguage == .zh ? 146:218)
            make.height.equalTo(30)
        }
        
    }
    private lazy var sliderView: JXSegmentedIndicatorGradientLineView = {//标题背景
        let sliderView = JXSegmentedIndicatorGradientLineView()
        sliderView.colors =  [UIColor(red: 1, green: 0.839, blue: 0, alpha: 1), UIColor(red: 0.851, green: 0.851, blue: 0.851, alpha: 0)]
        return sliderView
    }()
    lazy var dataSource: JXSegmentedTitleDataSource = {
        let ds = JXSegmentedTitleDataSource()
        ds.titles = titles
        ds.titleNormalColor =  .app_textGrayColor
        ds.titleSelectedColor = .app_black
        ds.titleNormalFont = kSetPingFangMedium(14)
        ds.titleSelectedFont = kSetPingFangMedium(14)
        ds.itemSpacing = 0
        return ds
    }()
    private(set) lazy var classifySegmentedView: JXSegmentedView = {
        let sv = JXSegmentedView(frame: CGRect.zero)
        sv.backgroundColor = .clear
        sv.dataSource = dataSource //数据源
        sv.indicators = [sliderView] //标题下的横线
        sv.contentEdgeInsetLeft = 0
        sv.contentEdgeInsetRight = 0
        sv.delegate = self //设置代理
        return sv
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension SearchClassifyCollectionReusableView: JXSegmentedViewDelegate {
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        guard let handle = selectClassifyHandle else { return }
        handle(titles[index], index)
    }
    
}

